package com.alpaak.common.utils.json;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * <p>标题: JsonTimestampValueProcessor</p>
 * <p>说明: json数据中Timestamp转换</p>
 * <p>时间: 2014年8月6日</p>
 *
 * @author zqb
 * @version 1.0
 */

public class JsonTimestampValueProcessor implements JsonValueProcessor {
    public JsonTimestampValueProcessor() {
        super();
    }

   
    public JsonTimestampValueProcessor(String format) {
        super();
    }

   
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value);
    }

   
    public Object processObjectValue(String key, Object value,
            JsonConfig jsonConfig) {
        return process(value);
    }

   
    private Object process(Object value) {
        try {
            if (value instanceof Timestamp) {
        		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        		try {
        			return sdf.format(value);
        		} catch (Exception e) {
        			return value.toString();
        		}            	
            }
            return value == null ? "" : value.toString();
        } catch (Exception e) {
            return "";
        }

    }
}